Write-Host "检查Agent Service依赖..." -ForegroundColor Green

# 检查Java
Write-Host "`n检查Java..." -ForegroundColor Yellow
try {
    $javaVersion = java -version 2>&1 | Select-String "version"
    if ($javaVersion) {
        Write-Host "✓ Java已安装: $javaVersion" -ForegroundColor Green
    } else {
        Write-Host "✗ Java未安装" -ForegroundColor Red
    }
} catch {
    Write-Host "✗ Java未安装" -ForegroundColor Red
}

# 检查Maven
Write-Host "`n检查Maven..." -ForegroundColor Yellow
try {
    $mavenVersion = mvn -version 2>&1 | Select-String "Apache Maven"
    if ($mavenVersion) {
        Write-Host "✓ Maven已安装: $mavenVersion" -ForegroundColor Green
    } else {
        Write-Host "✗ Maven未安装" -ForegroundColor Red
    }
} catch {
    Write-Host "✗ Maven未安装" -ForegroundColor Red
}

# 检查Redis
Write-Host "`n检查Redis..." -ForegroundColor Yellow
try {
    $redisResponse = redis-cli ping 2>&1
    if ($redisResponse -match "PONG") {
        Write-Host "✓ Redis运行中" -ForegroundColor Green
    } else {
        Write-Host "✗ Redis未运行 (响应: $redisResponse)" -ForegroundColor Red
    }
} catch {
    Write-Host "⚠ Redis未安装或未运行 (可选，但建议安装)" -ForegroundColor Yellow
    Write-Host "  安装方式:" -ForegroundColor Gray
    Write-Host "  1. Docker: docker run -d -p 6379:6379 --name redis redis:latest" -ForegroundColor Gray
    Write-Host "  2. WSL2: wsl -> sudo apt-get install redis-server" -ForegroundColor Gray
    Write-Host "  3. Chocolatey: choco install redis-64" -ForegroundColor Gray
}

# 检查Git
Write-Host "`n检查Git..." -ForegroundColor Yellow
try {
    $gitVersion = git --version 2>&1
    if ($gitVersion) {
        Write-Host "✓ Git已安装: $gitVersion" -ForegroundColor Green
    } else {
        Write-Host "⚠ Git未安装（可选）" -ForegroundColor Yellow
    }
} catch {
    Write-Host "⚠ Git未安装（可选）" -ForegroundColor Yellow
}

# 检查服务端口
Write-Host "`n检查服务端口..." -ForegroundColor Yellow

$services = @{
    "LLM Service" = @{ Port = 8084; Required = $true }
    "Code Analysis Service" = @{ Port = 8083; Required = $true }
    "Code Generation Service" = @{ Port = 8082; Required = $true }
    "Code Completion Service" = @{ Port = 8081; Required = $false }
    "Data Service" = @{ Port = 8085; Required = $false }
}

$allRequiredRunning = $true

foreach ($serviceName in $services.Keys) {
    $service = $services[$serviceName]
    $port = $service.Port
    $required = $service.Required
    
    try {
        $connection = Test-NetConnection -ComputerName localhost -Port $port -WarningAction SilentlyContinue -InformationLevel Quiet -ErrorAction SilentlyContinue
        if ($connection) {
            $status = if ($required) { "✓" } else { "✓" }
            Write-Host "$status $serviceName (端口 $port) 运行中" -ForegroundColor Green
        } else {
            $status = if ($required) { "✗" } else { "⚠" }
            $color = if ($required) { "Red" } else { "Yellow" }
            Write-Host "$status $serviceName (端口 $port) 未运行" -ForegroundColor $color
            if ($required) {
                $allRequiredRunning = $false
            }
        }
    } catch {
        $status = if ($required) { "✗" } else { "⚠" }
        $color = if ($required) { "Red" } else { "Yellow" }
        Write-Host "$status $serviceName (端口 $port) 未运行" -ForegroundColor $color
        if ($required) {
            $allRequiredRunning = $false
        }
    }
}

Write-Host "`n" -NoNewline
if ($allRequiredRunning) {
    Write-Host "✓ 所有必需服务运行正常！" -ForegroundColor Green
} else {
    Write-Host "✗ 部分必需服务未运行，请先启动相关服务" -ForegroundColor Red
    Write-Host "`n启动服务命令：" -ForegroundColor Yellow
    Write-Host "  cd llm-service && mvn spring-boot:run" -ForegroundColor Gray
    Write-Host "  cd code-analysis-service && mvn spring-boot:run" -ForegroundColor Gray
    Write-Host "  cd code-generation-service && mvn spring-boot:run" -ForegroundColor Gray
}

Write-Host "`n检查完成！" -ForegroundColor Green

